<template>
  <div>
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>

        <el-button
          style="float: right"
          @click="handleSearchList()"
          size="mini"
          type="primary"
        >
          查询结果
        </el-button>

        <el-button
          style="float: right; margin-right: 15px"
          @click="addDialogVisible = true"
          size="mini"
          type="success"
        >
          新增
        </el-button>
      </div>

      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="searchList"
          size="small"
          label-width="140px"
        >
          <el-form-item label="关键词:">
            <el-input
              style="width: 203px"
              v-model="searchList.key"
              placeholder="请输入要搜索的关键词"
              clearable
            ></el-input>
          </el-form-item>

          <el-form-item label="状态:">
            <el-select v-model="searchList.status" placeholder="请选择模板状态">
              <el-option label="" value=""></el-option>
              <el-option label="启用" value="NORMAL"></el-option>
              <el-option label="关闭" value="BAN"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <el-card style="margin-top: 15px" class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>

    <div style="margin-top: 15px" class="table-container">
      <el-table
        :data="templateList"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column label="关键词" width="350" align="center">
          <template slot-scope="scope">{{ scope.row.key }}</template>
        </el-table-column>
        <el-table-column label="回复信息" width="400" align="center">
          <template slot-scope="scope">{{ scope.row.message }}</template>
        </el-table-column>
        <el-table-column label="状态" width="350" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status == 'NORMAL'"
              :active-value="NORMAL"
              :inactive-value="BAN"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="handleChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="140" align="center">
          <template slot-scope="scope">
            <!-- 删除按钮 -->
            <el-button
              size="mini"
              type="warning"
              @click="handleDeleteList(scope.row)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        layout="prev, pager, next"
        :page-count="pageCunt"
        :current-page.sync="currentPage"
      >
      </el-pagination>
    </div>

    <el-dialog title="新增回复模板" :visible.sync="addDialogVisible">
      <el-form :model="template" label-width="100px">
        <!-- <el-form-item label="编号">
          <el-input
            v-model="template.id"
            placeholder="请输入模板编号"
            clearable
          ></el-input>
        </el-form-item> -->

        <el-form-item label="关键词">
          <el-input
            v-model="template.key"
            placeholder="请输入关键词"
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item label="回复信息">
          <el-input
            v-model="template.message"
            placeholder="请输入回复信息"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="template.status"
            :active-value="NORMAL"
            :inactive-value="BAN"
            active-color="#13ce66"
            inactive-color="#ff4949"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleAddList">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "AutoReplyTemplate",
  data() {
    return {
      // uploadURL: "http://localhost:8091/api/company/robotmsg",
      requestMethod: "POST",
      searchList: {
        key: "",
        status: "",
        pageNum: "1",
        pageSize: "5",
      },
      currentPage: 1,
      currenttemplateList: {
        pageNum: "1",
        pageSize: "5",
      },
      templateList: [
        {
          key: "",
          message: "",
          status: "",
        },
      ],
      editStatus: {
        id: "",
        status: "",
      },
      template: {
        key: "",
        message: "",
        status: 0,
      },

      // statusOptions: [
      //   { key: "NORMAL", value: "启用" },
      //   { key: "BAN", value: "禁用" },
      // ],
      addDialogVisible: false,
      listLoading: true,
      pageCunt: 1,
    };
  },
  created() {
    this.getList();
  },
  computed: {
    switchDisabled() {
      return false;
    },
  },
  methods: {
    getList() {
      this.listLoading = true;
      this.axios
        .get("/api/company/auth/robotMsg/list", {
          params: this.currenttemplateList,
        })
        .then((response) => {
          console.log(response);
          if (response.data.code == "200") {
            this.templateList = response.data.value.data;
            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            // this.currentPage = 1;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    // 设置搜索条件
    handleSearchList() {
      // console.log(this.searchList.key);
      // console.log(this.searchList.status);
      this.listLoading = true;
      this.axios
        .get("api/company/auth/robotMsg/list", {
          params: this.searchList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.templateList = response.data.value.data;
            var pcnt = response.data.value.total / 5;
            if (pcnt > Number.parseInt(pcnt)) {
              this.pageCunt = Number.parseInt(pcnt) + 1;
            } else {
              this.pageCunt = Number.parseInt(pcnt);
            }
            // this.currentPage = 1;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    // 重置搜索条件
    handleResetSearch() {
      this.searchList = {
        key: "",
        status: "",
        pageNum: "1",
        pageSize: "5",
      };
    },
    // 换页
    handleCurrentChange(val) {
      this.listLoading = true;
      this.currenttemplateList.pageNum = val;
      this.axios
        .get("/api/company/auth/robotMsg/list", {
          params: this.currenttemplateList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.templateList = response.data.value.data;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    // 增加模板
    handleAddList() {
      if (!this.template.key || !this.template.message) {
        this.$message.error("关键词和回复信息不能为空！");
      } else {
        this.template.status = this.template.status == true ? "NORMAL" : "BAN";
        console.log(this.template.status);
        this.$confirm("请再次确认是否添加").then(() => {
          this.axios
            .post(
              "/api/company/auth/robotMsg/insert?key=" +
                this.template.key +
                "&message=" +
                this.template.message +
                "&status=" +
                this.template.status
            )
            .then((response) => {
              if (response.data.code == "200") {
                this.$message({
                  message: "添加成功",
                  type: "success",
                });
                this.getList();
              } else {
                this.$message.error(response.data.message);
              }
            })
            .catch((error) => {
              this.$message.error("添加失败");
            });
        });
        this.addDialogVisible = false;
      }
    },
    // 删除模板
    handleDeleteList(row) {
      this.$confirm("请再次确认是否删除").then(() => {
        this.axios
          .post("/api/company/auth/robotMsg/delete?id=" + row.id)
          .then((response) => {
            if (response.data.code == "200") {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              this.getList();
            } else {
              this.$message.error(response.data.message);
            }
          })
          .catch((error) => {
            this.$message.error("删除失败");
          });
      });
    },

    handleChange(row) {
      const originalStatus = row.status;
      this.$confirm("请再次确认是否选择修改")
        .then(() => {
          row.status = row.status === "NORMAL" ? "BAN" : "NORMAL";
          this.axios
            .post(
              "/api/company/auth/robotMsg/update?id=" +
                row.id +
                "&status=" +
                row.status
            )
            .then((response) => {
              if (response.data.code == "200") {
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
              } else {
                row.status = originalStatus;
                this.$message.error(response.data.message);
              }
            })
            .catch((error) => {
              row.status = originalStatus;
              this.$message.error("修改失败");
            });
        })
        .catch(() => {
          row.status = originalStatus;
          this.$message.info("已取消修改");
        });
    },
  },
};
</script>
